//
// MaFenetre.cpp for  in /home/goldbe_y//cpp/plazza-2014-bramar_g/Qt
//
// Made by yoni goldberg
// Login   <goldbe_y@epitech.net>
//
// Started on  Sun Apr 17 16:46:17 2011 yoni goldberg
// Last update Wed May 11 16:43:28 2011 leo faucon
//

#include "MaFenetre.hpp"
#include "convert.hpp"

MaFenetre::MaFenetre(QApplication &app)
{
  (void)app;

  Parseur = NULL;

  setGeometry(500, 250, 900, 600);
  Bienvenue = new QLabel("");
  Consigne = new QLabel("Charger un fichier puis appuyer sur 'Calculer' : ");
  Quit = new QPushButton("Quit");
  calculer = new QPushButton("Calculer");
  charger = new QPushButton("Charger");
  graph = new QPushButton("graph");


  layout = new QGridLayout;
  for (int i = 0; i < 12; i++)
    value << new QLabel("");
  for (int i = 0; i < 12; i++)
    type << new QLabel("");
  for (int i = 0; i < 12; i++)
    type[i]->setText(QString(tab[i].c_str()));

  table = new QTableWidget(10,3, this);
  for (int j = 0; j < 3; j++)
    for (int i = 0; i < 10; i++)
      {
	item = new QTableWidgetItem();
	item->setText("0");
	item->setFlags(Qt::NoItemFlags);
	table->setItem(i,j,item);
      }
  initTable_prop();
  Place(app);
}

void		MaFenetre::Place(QApplication &app)
{
  layout->addWidget(Bienvenue, 0, 1);
  for (int i = 1; i <= 12; i++)
    layout->addWidget(type[i - 1],i - 1, 5);
  for (int i = 1; i <= 12; i++)
    layout->addWidget(value[i - 1],i - 1, 6);
  layout->addWidget(calculer, 6, 4);
  //  layout->addWidget(graph, 7, 3);
  layout->addWidget(table, 1, 1, 10, 3);
  layout->addWidget(Consigne, 11, 1, 1, 3, Qt::AlignHCenter);
  layout->addWidget(charger, 12, 2);
  layout->addWidget(Quit, 12, 6, 1, 2);
  QWidget::connect(Quit, SIGNAL(clicked()), &app, SLOT(quit()));
  QWidget::connect(calculer, SIGNAL(clicked()), this, SLOT(calc()));
  QWidget::connect(charger, SIGNAL(clicked()), this, SLOT(fct_table()));
  QWidget::connect(graph, SIGNAL(clicked()), this, SLOT(fct_graph()));
  setLayout(layout);
}

void		MaFenetre::initTable_prop()
{
  QStringList	header;
  header << "Observation" << "Effectif" << "Frequence";
  table->setHorizontalHeaderLabels(header);
  QHeaderView *Header_h = table->horizontalHeader();
  Header_h->setClickable(false);
  Header_h->setResizeMode(QHeaderView::ResizeToContents);
  Header_h->setResizeMode(QHeaderView::Stretch);
  table->verticalHeader()->setVisible(false);
}

void		MaFenetre::fct_modif(int row, int col)
{
  int			i = 0;
  bool			err = false;
  QTableWidgetItem	*item;

  QWidget::disconnect(table, SIGNAL(cellChanged(int, int)), 0, 0);
  item = table->item(row, col);
  std::string tmp = item->text().toStdString();
  while (tmp[i])
    {
      if (tmp[i] < '0' || tmp[i] > '9')
	{
	  if (col == 0)
	    item->setText(QString(var_to_string(Parseur->getval()[row], 0).c_str()));
	  else
	    item->setText(QString(var_to_string(Parseur->getcoef()[row], 0).c_str()));
	  err = true;
	  break;
	}
      i++;
    }
  if (!err)
    {
      int	new_val;
      string_to_var(new_val, tmp);
      if (col == 0)
  	Parseur->getval()[row] = new_val;
      else
  	Parseur->getcoef()[row] = new_val;
      item->setText(QString(tmp.c_str()));
    }
  else
    QMessageBox::critical(this, "Erreur", "Caracteres non numerique");
  QWidget::connect(table, SIGNAL(cellChanged(int,int)), this, SLOT(fct_modif(int, int)));
}

void		MaFenetre::calc()
{
  if (Parseur != NULL)
    {
      Maths     math (Parseur->getval(), Parseur->getcoef(), Parseur->getLine());
      std::vector<float> res = math.getres();
      for (int i = 0; i < 12; i++)
	{
	  std::string temp;
	  temp = var_to_string(res[i], 3);
	  value[i]->setText(QString(temp.c_str()));
	}
      QWidget::disconnect(table, SIGNAL(cellChanged(int, int)), 0, 0);
      for (int i = 0; i < Parseur->getLine(); i++)
	table->removeCellWidget(i,2);
      math.frequence();
      for (int i = 0; i < Parseur->getLine(); i++)
	{
	  item = new QTableWidgetItem();
	  item->setText(QString(var_to_string(math.getfreq()[i], 3).c_str()));
	  item->setFlags(Qt::NoItemFlags);
	  table->setItem(i,2,item);
	}
      QWidget::connect(table, SIGNAL(cellChanged(int,int)), this, SLOT(fct_modif(int, int)));
    }
}

void		MaFenetre::fct_table()
{
  if (Parseur != NULL)
    delete Parseur;

  QString fichier = QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QString(), "Fichier Texte(*.txt *.ol *.fe)");

  if (fichier == NULL)
    return;

  QWidget::disconnect(table, SIGNAL(cellChanged(int, int)), 0, 0);
  std::string tmp = fichier.toStdString();
  Parseur = new Parse(tmp.c_str());
  table->clearContents();
  table->setRowCount(Parseur->getLine());
  for (int i = 0; i < Parseur->getLine(); i++)
    {
      item = new QTableWidgetItem();
      item->setText(QString(var_to_string(Parseur->getval()[i], 0).c_str()));
      table->setItem(i,0,item);
    }
  for (int i = 0; i < Parseur->getLine(); i++)
    {
      item = new QTableWidgetItem();
      item->setText(QString(var_to_string(Parseur->getcoef()[i], 0).c_str()));
      table->setItem(i,1,item);
    }
  for (int i = 0; i < Parseur->getLine(); i++)
    {
      item = new QTableWidgetItem();
      item->setText(QString(""));
      item->setFlags(Qt::NoItemFlags);
      table->setItem(i,2,item);
    }
  QWidget::connect(table, SIGNAL(cellChanged(int,int)), this, SLOT(fct_modif(int, int)));
}

void	MaFenetre::fct_graph()
{

}
